<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
use think\Request;

class Finance extends Backend
{
    /**
     * 平台资产规划管理
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            // 获取权益积分统计信息
            $equityStats = $this->getEquityStats();
            // 获取绿色积分统计信息
            $greenStats = $this->getGreenStats();
            
            $this->success('获取成功', null, [
                'equity' => $equityStats,
                'green' => $greenStats
            ]);
        }
        
        return $this->view->fetch();
    }
    
    /**
     * 获取权益积分统计信息
     */
    private function getEquityStats()
    {
        // 平台当前总锁仓权益积分总额
        $totalLocked = Db::name('user_points')
            ->where('type', 4) // 权益积分类型
            ->where('status', 1) // 锁定状态
            ->sum('points');
            
        // 平台当前可用权益积分总额
        $totalAvailable = Db::name('user_points')
            ->where('type', 4)
            ->where('status', 0) // 可用状态
            ->sum('points');
            
        // 平台当前已使用权益积分总额
        $totalUsed = Db::name('user_points_log')
            ->where('type', 4)
            ->where('points', '<', 0) // 消费记录
            ->sum('points');
            
        // 平台当前待到账权益积分总额
        $totalPending = Db::name('user_points')
            ->where('type', 4)
            ->where('status', 2) // 待到账状态
            ->sum('points');
            
        // 从配置表获取权益积分相关配置
        $config = Db::name('config')
            ->whereIn('name', [
                'equity_exchange_ratio', // 权益积分转换金额比例
                'equity_withdraw_fee', // 提现费率
                'equity_valid_days', // 权益有效期天数
                'equity_release_ratio', // 权益积分释放比例
                'green_release_ratio', // 绿色积分释放比例
                'equity_certificate_max' // 权益凭证最大额度
            ])
            ->column('value', 'name');
            
        return [
            'total_locked' => $totalLocked ?: 20000,
            'total_available' => $totalAvailable ?: 2000000,
            'total_used' => abs($totalUsed) ?: 20000,
            'total_pending' => $totalPending ?: 20000,
            'exchange_ratio' => $config['equity_exchange_ratio'] ?? 0.8,
            'withdraw_fee' => $config['equity_withdraw_fee'] ?? 0.8,
            'valid_days' => $config['equity_valid_days'] ?? 200,
            'release_ratio' => $config['equity_release_ratio'] ?? 0.8,
            'green_release_ratio' => $config['green_release_ratio'] ?? 0.8,
            'certificate_max' => $config['equity_certificate_max'] ?? 200000
        ];
    }
    
    /**
     * 获取绿色积分统计信息
     */
    private function getGreenStats()
    {
        // 平台当前可用绿色积分总额
        $totalAvailable = Db::name('user_points')
            ->where('type', 5) // 绿色积分类型
            ->where('status', 0) // 可用状态
            ->sum('points');
            
        // 平台当前已使用绿色积分总额
        $totalUsed = Db::name('user_points_log')
            ->where('type', 5)
            ->where('points', '<', 0) // 消费记录
            ->sum('points');
            
        return [
            'total_available' => $totalAvailable ?: 2000000,
            'total_used' => abs($totalUsed) ?: 20000
        ];
    }
    
    /**
     * 更新权益积分配置
     */
    public function updateEquityConfig()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();
            
            $configMap = [
                'exchange_ratio' => 'equity_exchange_ratio',
                'withdraw_fee' => 'equity_withdraw_fee', 
                'valid_days' => 'equity_valid_days',
                'release_ratio' => 'equity_release_ratio',
                'green_release_ratio' => 'green_release_ratio',
                'certificate_max' => 'equity_certificate_max'
            ];
            
            Db::startTrans();
            try {
                foreach ($configMap as $key => $configName) {
                    if (isset($params[$key])) {
                        Db::name('config')
                            ->where('name', $configName)
                            ->update(['value' => $params[$key]]);
                    }
                }
                
                Db::commit();
                $this->success('配置更新成功');
            } catch (\Exception $e) {
                Db::rollback();
                $this->error('配置更新失败：' . $e->getMessage());
            }
        }
    }
}
